import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import s from './s';
import init from '@feidao/web/framework/browser/dn/component';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';
import uploader from '../atom/upload';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';
import doc_code from '@feidao/web/atom/browser/msg/doc-code';

let can_add: boolean = true, form_add: HTMLFormElement = null, base64Code: string = null,
	upload: HTMLInputElement = null, pic: object = {};

const feidao = {};

const daoke = {
	async init(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('init');
		upload = fd.data.node.querySelector('#uploader');
	},
	async ec01(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-002 b init', args);
		const fzno = args[0][0];
		form_add = fd.data.node.querySelector('#form_add') as HTMLFormElement;
		form_add.classroom_id.value = fd.data.params.classroom_id;
		form_add.fzno.value = fzno;
		form_add.comm_picture_name.value = "";
		const img = fd.data.node.querySelector('[data-feidao-id="img"]') as HTMLImageElement;
		img.src = "";
		fd.data.node.classList.remove('display-n');
		base64Code = '';
		can_add = true;
	},
	async eu02(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-002 b click close', args);
		fd.data.node.classList.add('display-n');
	},
	async eu03(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-002 b click save', args);
		pic['classroom_id'] = form_add.classroom_id.value;
		pic['fzno'] = Number(form_add.fzno.value);
		pic['comm_picture_name'] = form_add.comm_picture_name.value;
		pic['comm_picture_no'] = (await doc_code('flippedclass', '12', 'BM0010013', '')).result;
		pic['house_name'] = fd.data.params.house_name;
		pic['house_number'] = fd.data.params.house_number;
		const res = await nodejs("flippedclass", "class_room_picture_add", { rowdata: pic });
		if (res == 'success') {
			alert("操作成功");
			fd.data.node.classList.add('display-n');
			fire_component_msg('zj-reality-picture-001', 'ec04', 'add');
		} else {
			alert("操作失败");
		}
	},
	async ec04(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-002 b uploaded', args);
		const file = upload.files[0];
		const reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = function (e) {
			const img = fd.data.node.querySelector('[data-feidao-id="img"]') as HTMLImageElement;
			base64Code = this.result;
			img.src = base64Code;
		}
	},
	async eu05(fd: IFeidaoAiBrowserComponent, ...args: any[]) {
		console.log('zj-reality-picture-002 b click 上传', args);
		if (!base64Code) {
			alert("请先浏览选择一张图片");
			return;
		}
		try {
			const res = await uploader('flippedclass', base64Code);
			if (res != 'fail') {
				pic['pic_path'] = res['filename'];
			}
		} catch (error) {
			console.error(error);
		}
	}
};

const rules = {};

export default function main(url: string, query: any) {
	return init('zj-reality-picture-002', s, rules, feidao, daoke, url, query, { spaceid: 'flippedclass', systemid: '5991', refsystemid: '@refsystemid' });
}
